package com.beiing.tianshuai.tianshuai.service;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.beiing.tianshuai.tianshuai.activity.MainActivity;
import com.beiing.tianshuai.tianshuai.dongtai.DynamicFragment;
import com.beiing.tianshuai.tianshuai.dongtai.ui.DynamicMessageActivity;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

/**
 * 使用通知
 * Created by magikliu on 2017/9/30.
 */

public class JPushReceiver extends BroadcastReceiver {

    private static String TAG = "JPushReceiver";
    private NotificationManager nm;

    @Override
    public void onReceive(Context context, Intent intent) {
        nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
                //JPush用户注册成功
                LogUtils.i("注册极光推送成功", TAG);
            } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                //接收到推送下来的自定义消息
                LogUtils.i("接收到推送下来的自定义消息" + bundle.getString(JPushInterface.EXTRA_MESSAGE), TAG);
            } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
                //接收到推送下来的通知 (在这里处理)
                LogUtils.i("=========================================================", TAG);
                LogUtils.i("接收到推送下来的通知 title    " + bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE), TAG);
                LogUtils.i("接收到推送下来的通知 msg      " + bundle.getString(JPushInterface.EXTRA_ALERT), TAG);
                LogUtils.i("接收到推送下来的通知 type     " + bundle.getString(JPushInterface.EXTRA_ALERT_TYPE), TAG);
                LogUtils.i("接收到推送下来的通知 id       " + bundle.getString(JPushInterface.EXTRA_MSG_ID), TAG);
                LogUtils.i("接收到推送下来的通知 id       " + bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID), TAG);

                try {
                    MainActivity.updateDynamicNewMessagePoint(context);
                    DynamicFragment.setNewMessagePointState();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                //用户点击打开了通知
                Intent i = new Intent(context, DynamicMessageActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);
                JPushInterface.clearAllNotifications(context);
            }
        }
    }

    private void receivingNotification(Context context, Bundle bundle) {

    }

    private void openNotification(Context context, Bundle bundle) {
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        String myValue = "";
        JSONObject extrasJson = null;
        try {
            extrasJson = new JSONObject(extras);
            myValue = extrasJson.optString("myKey");
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

}
